package cn.tedu.truetaste.controller;

import cn.tedu.truetaste.pojo.dto.StoreAddNewDTO;
import cn.tedu.truetaste.pojo.dto.StoreUpdateDTO;
import cn.tedu.truetaste.pojo.vo.StoreListItemVO;
import cn.tedu.truetaste.pojo.vo.StoreStandardVO;
import cn.tedu.truetaste.service.IStoreService;
import cn.tedu.truetaste.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/stores")
@Api(tags = "店铺管理模块")
public class StoreController {
    @Autowired
    private IStoreService storeService;

    public StoreController() {
        log.info("创建控制器对象：StoreController");
    }

    // http://localhost:9989/stores/add-new
    @ApiOperation("添加店铺")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(StoreAddNewDTO storeAddNewDTO) {
        log.debug("开始处理【添加店铺】的请求，参数：{}", storeAddNewDTO);
        storeService.addNew(storeAddNewDTO);
        return JsonResult.ok();
    }
    // http://localhost:9989/stores/delete
    @ApiOperation("删除店铺")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "店铺id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@PathVariable Long id) {
        log.debug("开始处理【删除店铺】的请求，参数：{}", id);
        storeService.delete(id);
        return JsonResult.ok();
    }


    // http://localhost:9989/stores/update
    @ApiOperation("修改店铺详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "店铺id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult<Void> updateById(@PathVariable Long id, StoreUpdateDTO storeUpdateDTO) {
        log.debug("开始处理【修改店铺详情】的请求，参数ID：{}, 新数据：{}", id, storeUpdateDTO);
        storeService.updateInfoById(id, storeUpdateDTO);
        return JsonResult.ok();
    }


    @ApiOperation("根据id查询店铺详情")
    @ApiOperationSupport(order = 400)
    @GetMapping("/{id:[0-9]+}")
    public JsonResult<StoreStandardVO> getStandardById(@PathVariable Long id) {
        log.debug("开始处理【根据id查询店铺详情】的请求，参数：{}", id);
        StoreStandardVO store = storeService.getStandardById(id);
        return JsonResult.ok(store);
    }
    // http://localhost:9989/stores/
    @ApiOperation("查询店铺列表")
    @ApiOperationSupport(order = 420)
    @GetMapping("")
    public JsonResult<List<StoreListItemVO>> list() {
        log.debug("开始处理【查询店铺列表】的请求，无参数");
        List<StoreListItemVO> list = storeService.list();
        return JsonResult.ok(list);
    }

}
